/**
 * @description 博客相关api
 * @author 印明帝
 */

const router = require('koa-router')()
const xss = require('xss')
const { blogValidate } = require('../../validator/blog')
const { genValidator } = require('../../middleware/validator')
const { loginCheck } = require('../../middleware/loginCheck')
const { createBlog,getBlogList } = require('../../control/blog')

router.prefix('/api/blog')

// 创建博客
router.post('/createBlog',loginCheck,genValidator(blogValidate),async (ctx,next)=>{
  let { content,image } = ctx.request.body
  let { id:userId } = ctx.session.userInfo
  // xss攻击预防
  content = xss(content)
  // 创建用户
  ctx.body = await createBlog({ userId,content,image })
})

// 获取博客列表
router.get('/getBlogList',loginCheck, async (ctx,next) => {
  //  获取查询信息
  let { id,index } = ctx.query
  // 调用函数
  ctx.body = await getBlogList({ ctx,id,index })
})

module.exports = router
